package com.ivo.biz;


import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.quartz.LcmPerformanceQuartz;
import com.ivo.sec.SessionLocal;
import com.ivo.util.JsonUtil;


@RestController
@RequestMapping("/lcm/performance")
public class LcmPerformanceController {
	
	@Autowired
	private PerformanceService performanceService;
	
	@Autowired
    private LcmPerformanceQuartz lcmPerformanceQuartz;
	
	@RequestMapping(value = "/LcmEPerformance")
	@ResponseBody
	public ModelAndView getLcmEPerformance(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmEPerformance");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/LcmMPerformance")
	@ResponseBody
	public ModelAndView getLcmMPerformance(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmMPerformance");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/LcmAttendenceThisMon")
	@ResponseBody
	public ModelAndView getAttendence(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmAttendenceThisMon");
		return mv;
		
	}

	@RequestMapping(value = "/LcmCapacityOfThisMon")
	@ResponseBody
	public ModelAndView getActualCapacity(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmCapacityOfThisMon");
		return mv;
		
	}
	
	@RequestMapping(value = "/LcmCompAtibility")
	@ResponseBody
	public ModelAndView getCompAtibility(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmCompAtibility");
		return mv;
		
	}
	
	@RequestMapping(value = "/LcmQualityThisMon")
	@ResponseBody
	public ModelAndView getQuality(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmQualityThisMon");
		return mv;
		
	}
	
	@RequestMapping(value = "/LcmDiscipline")
	@ResponseBody
	public ModelAndView getDiscipline(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmDiscipline");
		return mv;
		
	}
	
	@RequestMapping(value = "/LcmLearns")
	@ResponseBody
	public ModelAndView getLcmLearns(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmLearns");
		return mv;
		
	}
	
	@RequestMapping(value = "/LcmProfessional")
	@ResponseBody
	public ModelAndView getProfessional(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","LcmProfessional");
		return mv;
		
	}
	

	
	@RequestMapping(value = "/LcmDustFree")
	@ResponseBody
	public ModelAndView getDustFree(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","dustfree");
		return mv;
		
	}
	
	@RequestMapping(value = "/majorAddition")
	@ResponseBody
	public ModelAndView majorAddition(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/lcm");
		mv.addObject("category","majorAddition");
		return mv;
		
	}	

	
	  
 
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public String getList(
    		HttpServletRequest request,
    		//@RequestParam(value = "allFlag", required = false) int allFlag,
    		//@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "score", required = false) String score,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "scope", required = false) String scope,
    		@RequestParam(value = "ope_fk", required = false) String ope_id,
    		@RequestParam(value = "jsp", required = false) String jsp,
    		@RequestParam(value = "bonus", required = false) String bonus
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getLCMPerformance(emp_id,month,ope_id,score,bonus,scope,classtype,jsp));
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	
	
	
	
	
	
    
	
    
	
    
    
	
    
    
    
   
    
	
	@RequestMapping(value = "/getListAttendenceThisMon")
	@ResponseBody
	public String getListAttendenceThisMon(
    		HttpServletRequest request,
    		//@RequestParam(value = "allFlag", required = false) int allFlag,
    		//@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "scope", required = false) String scope,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListLcmAttendenceThisMon(emp_id,month,ope_id,scope,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	@RequestMapping(value = "/getListCapacityThisMon")
	@ResponseBody
	public String getListCapacityThisMon(
    		HttpServletRequest request,
    		//@RequestParam(value = "allFlag", required = false) int allFlag,
    		//@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "scope", required = false) String scope,
    		@RequestParam(value = "ope_id[]", required = false) String[] ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListCapacityThisMon(emp_id,month,scope,ope_id,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/getListQualityThisMon")
	@ResponseBody
	public String getListQualityThisMon(
    		HttpServletRequest request,
    		//@RequestParam(value = "allFlag", required = false) int allFlag,
    		//@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "scope", required = false) String scope,
    		@RequestParam(value = "category", required = false) String category,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListQualityThisMon(emp_id,month,ope_id,scope,category,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/getListCompAtibility")
	@ResponseBody
	public String getListCompAtibility(
    		HttpServletRequest request,
    		//@RequestParam(value = "allFlag", required = false) int allFlag,
    		//@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "scope", required = false) String scope,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "category", required = false) String category,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListCompAtibility(emp_id,month,ope_id,scope,category,classtype));
        return JsonUtil.toJson(returnMap);
    }
	@RequestMapping(value = "/getListDiscipline")
	@ResponseBody
	public String getListDiscipline(
    		HttpServletRequest request,
    		//@RequestParam(value = "allFlag", required = false) int allFlag,
    		//@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "scope", required = false) String scope,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListDiscipline(emp_id,month,ope_id,scope,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/getListLearns")
	@ResponseBody
	public String getListLearns(
    		HttpServletRequest request,
    		//@RequestParam(value = "allFlag", required = false) int allFlag,
    		//@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "scope", required = false) String scope,
    		@RequestParam(value = "category", required = false) String category,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListLearns(emp_id,month,ope_id,scope,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/getListProfessional")
	@ResponseBody
	public String getListProfessional(
    		HttpServletRequest request,
    		//@RequestParam(value = "allFlag", required = false) int allFlag,
    		//@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "scope", required = false) String scope,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "category", required = false) String category,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListProfessional(emp_id,month,ope_id,scope,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/getOperation")
	@ResponseBody
	public String getOperation(HttpServletRequest request,@RequestParam(value = "category", required = false) long category) throws ParseException{
		Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        if(category == 25){
        returnMap.put("LcmOperation", performanceService.getOperation(category));
        }else if(category == 00000){
        	returnMap.put("CelOperation", performanceService.getOperation(category));
        }else if(category == 5){
        	returnMap.put("AryOperation", performanceService.getOperation(category));
        }else if(category == 0){
        	returnMap.put("LcmOperation", performanceService.getOperation(category));
        }else if(category == 24){
        	returnMap.put("M1", performanceService.getOperation(category));
        }else if(category == 25){
        	returnMap.put("M2", performanceService.getOperation(category));
        }
        return JsonUtil.toJson(returnMap);
		
	}
	
	/*@RequestMapping(value = "/getBonusGap")
    @ResponseBody
    public String getBonusGap(
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_fk", required = false) Long ope_fk,
    		@RequestParam(value = "scope", required = false) String scope
    		) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getBonusGap(month,ope_fk,scope));
        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }*/
	
	@RequestMapping(value = "/getAllBonuses")
    @ResponseBody
    public String getAllBonuses(
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_fk", required = false) Long ope_fk,
    		@RequestParam(value = "jsp", required = false) String jsp,
    		@RequestParam(value = "scope", required = false) String scope
    		) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result",performanceService.getLcmAllBonuses(month,ope_fk,scope,jsp));
        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/getActualBonus")
    @ResponseBody
    public String getActualBonus(
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_fk", required = false) Long ope_fk,
    		@RequestParam(value = "jsp", required = false) String jsp,
    		@RequestParam(value = "scope", required = false) String scope
    		) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result",performanceService.getLcmActualBonus(month,ope_fk,scope,jsp));
        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/syschron", method = RequestMethod.POST)
    @ResponseBody
    public String syschron(HttpServletRequest request,
    		@RequestParam(value = "month") String month) {
		lcmPerformanceQuartz.Calculate(month);
		//lcmPerformanceQuartz.CalculateBonus(month);
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	
	@RequestMapping(value = "/updateBonus")
    @ResponseBody
    public String updateBonus(HttpServletRequest request,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	  List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
		
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.updateBonus(list));
        
        
        
        
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	
	@RequestMapping(value = "/updateCapacityByMon")
    @ResponseBody
    public String updateCapacityByMon(HttpServletRequest request,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	  List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
		
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.updateCapacityByMon(list));
        
        
        
        
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/updateCompatibilityByMon")
    @ResponseBody
    public String updateCompatibilityByMon(HttpServletRequest request,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	  List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
		
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.updateCompatibilityByMon(list));
        
        return JsonUtil.toJson(returnMap);
    }
	
	
	
    @RequestMapping(value = "/updateQuality")
    @ResponseBody
    public String updateQuality(HttpServletRequest request,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	  List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
		
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.updateQuality(list));
        
        return JsonUtil.toJson(returnMap);
    }
    
    
	
	@RequestMapping(value = "/updateDiscipline")
    @ResponseBody
    public String updateDiscipline(HttpServletRequest request,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	  List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
		
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.updateDiscipline(list));
        
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	@RequestMapping(value = "/updateLearns")
    @ResponseBody
    public String updateLearns(HttpServletRequest request,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	  List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
		
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.updateLearns(list));
        
        return JsonUtil.toJson(returnMap);
    }
	
	
    
	@RequestMapping(value = "/updateProfessional")
    @ResponseBody
    public String updateProfessional(HttpServletRequest request,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	  List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
		
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.updateProfessional(list));
        
        return JsonUtil.toJson(returnMap);
    }
    
    
	
	 @RequestMapping(value = "/manufacturingOrEngineering", method = RequestMethod.POST)
	    @ResponseBody
	    public String manufacturingOrEngineering(HttpServletRequest request) {
	        String err_code = "";
	    	String err_msg = "";
	    	String fab = "阿凡达";
	    	if(SessionLocal.getUser()!=null)
	    		fab = SessionLocal.getUser().getFactory();
	    	if(fab.contains("制造")){
	    		err_code = "1";
	    		err_msg = "true";
	    	}else if(fab.contains("工程")){
	    		err_code = "0";
	    		err_msg = "false";
	    	}else{
	    		err_code = "2";
	    		err_msg = "true";
	    	}
	    	Map<String, Object> returnMap = new HashMap<String, Object>();
	    	returnMap.put("err_code", err_code);
	        returnMap.put("err_msg", err_msg);
	        return JsonUtil.toJson(returnMap);
	    }
	
	

}
